package com.bdqn.demo;

/**
 * SysUserControllerTester
 *
 * @author f
 * @since 2021/11/30
 */

import org.junit.Before;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.Resource;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SysUserControllerTester {
     @Resource
     private WebApplicationContext wac;//注入 Spring Web 上下文的环境
     private MockMvc mockMvc;

     @Before
     public void setup() {
         this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
     }

     @Test
     public void testGetSysUser() throws Exception{
         String mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/user/2"))
                 .andReturn().getResponse().getContentAsString();
         System.out.println("Result === " + mvcResult);
     }

     public void testAddSysUser() throws Exception{
         //构造请求参数
         final MultiValueMap<String ,String > params = new LinkedMultiValueMap<>();
         params.add("usrName","RESTful");
         params.add("usrPassword","123456");
         params.add("usrRoleId","2");
         params.add("usrFlag","1");
         mockMvc.perform(MockMvcRequestBuilders.post("/user").params(params))//指定参数
                 .andExpect(MockMvcResultMatchers.status().isOk())//期待返回状态码为200
                 .andDo(MockMvcResultHandlers.print());//打印返回的响应信息
     }
}
